package at.fhj.app.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Locale;

//import android.util.Log;

public class FhpiRetriever {
	
	public FhpiRetriever(){
		// without setting user data, the functionality is limited
	}
	
	public String getStudents(String course, String year){
		StringBuffer sb = new StringBuffer();
		//Log.i("FhpiRetriever", "Retrieving data from FHPI using locale "+Locale.getDefault()+".");
		String init_url = Configuration.API_ROOT + "getStudents.php?locale="+Locale.getDefault()+"&course="+URLEncoder.encode(course)+"&year="+URLEncoder.encode(year);
		//Log.i("FhpiRetriever", init_url);
		try {
			BufferedReader inreader = null;
			URL url = new URL(init_url);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				inreader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
				String line;
				while ((line = inreader.readLine()) != null) {
					sb.append(line);
				}
				inreader.close();
			}
			conn = null;
		} catch (Exception e) {
			e.printStackTrace();
			sb.append(e.getMessage());
		}
		
		return sb.toString();
	}

}